package com.iqiyi.pps.epg.web.webapp.action.search;

import com.iqiyi.kiwi.utils.Configuration;
import com.iqiyi.pps.epg.core.model.channel.ChannelBoss;
import com.iqiyi.pps.epg.core.model.channel.ChannelConfig;
import com.iqiyi.pps.epg.core.model.channel.ChannelTag;
import com.iqiyi.pps.epg.core.model.control.PlatformControl;
import com.iqiyi.pps.epg.core.service.broadcast.BroadcastControlService;
import com.iqiyi.pps.epg.core.service.channel.ChannelService;

import com.iqiyi.pps.epg.core.utils.DateTimeUtil;
import com.iqiyi.pps.epg.web.utils.Struts2Utils;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import java.util.*;

@Namespace("/search/search")
@Scope("prototype")
public class SearchAction extends ActionSupport {
    @Autowired
    private ChannelService channelService;
    @Autowired
    private BroadcastControlService broadcastControlService;

    private String apiHost = Configuration.getString("local.api.host");

    private String id;
    private ChannelBoss channelBoss;
    private List<Map<String, String>> channelTag;
    private List<Map<String, String>> platformList = new ArrayList();
    private List<ChannelConfig> locationConfigList;
    private Map<String, Set<Integer>> blacklist = new HashMap();

    public String getApiHost() {
        return apiHost;
    }

    public String getId() {
        return id;
    }

    public ChannelBoss getChannelBoss() {
        return channelBoss;
    }

    public List<Map<String, String>> getChannelTag() {
        return channelTag;
    }

    public List<Map<String, String>> getPlatformList() {
        return platformList;
    }

    public List<ChannelConfig> getLocationConfigList() {
        return locationConfigList;
    }

    public Map<String, Set<Integer>> getBlacklist() {
        return blacklist;
    }

    @Action(value = "channelInfo", results = {@Result(name = "success", location = "/WEB-INF/content/search/channelInfo.jsp")})
    public String channelInfos() {
        id = Struts2Utils.getParameter("id");
        List<Map<String, String>> channelTagData = new ArrayList<Map<String, String>>();
        if (StringUtils.isNotBlank(id)) {
            channelBoss = channelService.getChannelBoss(Long.parseLong(id));
            List<ChannelTag> channelTagTemp = channelService.getChannelTag(Long.parseLong(id));

            for (int i = 0; i < channelTagTemp.size(); i++) {
                Map<String, String> temp = new HashMap<String, String>();
                temp.put("id", channelTagTemp.get(i).getId()+"");
                temp.put("tagOne", channelService.getTagName(0L, channelTagTemp.get(i).getTag1()));
                temp.put("tagTwo", channelService.getTagName(channelTagTemp.get(i).getTag1(), channelTagTemp.get(i).getTag2()));
                temp.put("addTime", DateTimeUtil.transLongFormat(channelTagTemp.get(i).getAddTime().getTime()));
                channelTagData.add(temp);
            }

            // 播控信息
            List<ChannelConfig> platformConfigList = channelService.getConfig(ChannelConfig.PLAY_PLATFORM);
            locationConfigList = channelService.getConfig(ChannelConfig.LOCATION);
            List<PlatformControl> platformControlList = broadcastControlService.getPlatformControls(Long.valueOf(id));
            Map<Integer, Map> platformMap = new HashMap();

            if (platformControlList != null) {
                for (PlatformControl item : platformControlList) {
                    Map<String, String> temp = new HashMap();
                    temp.put("isChecked", "1");
                    temp.put("availableStatus", item.getAvailableStatus()+"");
                    temp.put("liveDelayTime", item.getLiveDelayTime()+"");
                    if (StringUtils.isNotEmpty(item.getBlacklist())) {
                        List<Integer> bl = new ArrayList();
                        for (String str : item.getBlacklist().split(",")) {
                            bl.add(Integer.valueOf(str));
                        }
                        blacklist.put(item.getPlayPlatform()+"", new HashSet(bl));
                    }
                    platformMap.put(item.getPlayPlatform(), temp);
                }
            }
            for (ChannelConfig item : platformConfigList) {
                Map<String, String> temp = new HashMap();
                if (platformMap.containsKey(item.getKey())) {
                    temp = platformMap.get(item.getKey());
                } else {
                    temp.put("isChecked", "0");
                    temp.put("availableStatus", "0");
                    temp.put("liveDelayTime", "0");
                }
                temp.put("id", item.getId()+"");
                temp.put("platformKey", item.getKey().toString());
                temp.put("platform", item.getDescr());
                platformList.add(temp);
            }
        }

        channelTag = channelTagData;
        return SUCCESS;
    }
}
